home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Demo ƒ / HeartQuest demo ƒ / CenterStuff.p next >
Text File  |  1995-02-24  |  5KB  |  165 lines

  1. unit CenterStuff;
  2.  
  3. interface
  4. {$IFC UNDEFINED THINK_PASCAL}
  5.     uses
  6.         Types, Quickdraw, Dialogs, Windows, Resources;
  7. {$ENDC}
  8.  
  9.     procedure CenterDialog (id: integer);
  10.     procedure CenterPDialog (id: integer; var corner: Point);
  11.     procedure CenterRect (var r: Rect);
  12.     function DoAlert (kind, alertID: integer; filterProc: ProcPtr): integer;
  13.  
  14. implementation
  15.  
  16. {Based on code from the following post:}
  17.  
  18. {From: Michael_Hecht@mac.sas.com (Michael Hecht)}
  19. {Newsgroups: comp.sys.mac.programmer}
  20. {Subject: Re: How to tell if a window has a title bar?}
  21. {Date: 26 Mar 92 20:03:05 GMT}
  22. {Here's how I compute the height of a window's title bar: …}
  23.  
  24.     function GetTitleHeight (theWindow: WindowPtr): integer;
  25.         var
  26.             windRect: Rect;
  27.             bias: integer;
  28.     begin
  29.     { Convert window's portRect to global coordinates }
  30.         windRect := theWindow^.portRect;
  31.         LocalToGlobal(windRect.topLeft);
  32.         LocalToGlobal(windRect.botRight);
  33.  
  34.     { Calculate the height of the window's title bar }
  35.         bias := windRect.top - 1 - WindowPeek(theWindow)^.strucRgn^^.rgnBBox.top;
  36.         windRect.top := windRect.top - bias;
  37. { What is the result? windRect? [I HAVN'T PORTED THIS TO WORKING CONDITION! /Ingemar]}
  38.  
  39. {How about this as a simple solution: just take difference between contents height}
  40. {and total height!}
  41.     with WindowPeek(theWindow)^.strucRgn^^ do
  42.         with theWindow^ do
  43.             GetTitleHeight:=(rgnBBox.bottom - rgnBBox.top) - (portRect.bottom - portRect.top)
  44.     end;
  45.  
  46. {> (Or, just in case I've lost the woods for the trees, how do I accurately}
  47. {> center my windows?)}
  48.  
  49. {Here's what I do:}
  50.  
  51. { CenterRect:    Center a rectangle on the main screen }
  52.     procedure CenterRect (var r: Rect);
  53.         var
  54.             delta, screenTop: integer;
  55.     begin
  56.     { Center it horizontally }
  57.         delta := r.right - r.left;
  58. {$IFC UNDEFINED THINK_PASCAL}
  59.         r.left := BSR(qd.screenBits.bounds.right - delta, 1);
  60. {$ELSEC}
  61.         r.left := BSR(screenBits.bounds.right - delta, 1);
  62. {$ENDC}
  63.         r.right := r.left + delta;
  64.  
  65.     { Determine top of screen }
  66.         screenTop := 22; {GetMBarHeight ( ); { I'm cheating a bit here - GetMBarHeight would be better. /Ingemar }
  67.  
  68.     { Place it in upper third of screen }
  69.         delta := r.bottom - r.top;
  70. {$IFC UNDEFINED THINK_PASCAL}
  71.         r.top := (qd.screenBits.bounds.bottom - screenTop - delta) div 3 + screenTop;
  72. {$ELSEC}
  73.         r.top := (screenBits.bounds.bottom - screenTop - delta) div 3 + screenTop;
  74. {$ENDC}
  75.         r.bottom := r.top + delta;
  76.     end;
  77.  
  78.     procedure CenterDialog (id: integer);
  79.         var
  80.             theDLOG: DialogTHndl;
  81.             bounds: Rect;
  82.     begin
  83.     { Get the DLOG resource }
  84.         theDLOG := DialogTHndl(GetResource('DLOG', id));
  85.         if (theDLOG = nil) then
  86.             exit(CenterDialog);
  87.  
  88.     { Center it within screenBits }
  89.         bounds := theDLOG^^.boundsRect;
  90.         CenterRect(bounds);
  91.         theDLOG^^.boundsRect := bounds;
  92.     end; {CenterDialog}
  93.  
  94.     procedure CenterPDialog (id: integer; var corner: Point);
  95.         var
  96.             theDLOG: DialogTHndl;
  97.             bounds: Rect;
  98.     begin
  99.     { Get the DLOG resource }
  100.         theDLOG := DialogTHndl(GetResource('DLOG', id));
  101.         if (theDLOG = nil) then
  102.             exit(CenterPDialog);
  103.  
  104.     { Center it within screenBits }
  105.         bounds := theDLOG^^.boundsRect;
  106.         CenterRect(bounds);
  107.         theDLOG^^.boundsRect := bounds;
  108.  
  109.         if (longint(corner) <> 0) then
  110.             corner := bounds.topLeft;
  111.     end;
  112.  
  113.     procedure CenterAlert (id: integer);
  114.         var
  115.             theALRT: AlertTHndl;
  116.             bounds: Rect;
  117.     begin
  118.     { Get the ALRT resource }
  119.         theALRT := AlertTHndl(GetResource('ALRT', id));
  120.         if (theALRT = nil) then
  121.             exit(CenterAlert);
  122.  
  123.     { Center it within screenBits }
  124.         bounds := theALRT^^.boundsRect;
  125.         CenterRect(bounds);
  126.         theALRT^^.boundsRect := bounds;
  127.     end;
  128.  
  129.     function DoAlert (kind, alertID: integer; filterProc: ProcPtr): integer;
  130.         var
  131.             item: integer;
  132.     begin
  133.         CenterAlert(alertID);
  134.         InitCursor;        { Equivalent to SetCursor(&arrow);}
  135.         case kind of
  136.             stopIcon: 
  137.                 item := StopAlert(alertID, filterProc);
  138.             noteIcon: 
  139.                 item := NoteAlert(alertID, filterProc);
  140.             cautionIcon: 
  141.                 item := CautionAlert(alertID, filterProc);
  142.             otherwise
  143.                 item := Alert(alertID, filterProc);
  144.         end;
  145.         DoAlert := item;
  146.     end;
  147.  
  148.  
  149. {More from the original posting (but don't blame him for errors I've introduced!):}
  150.  
  151. {For dialogs, I call CenterDialog. It will optionally hand me back the top,left}
  152. {coordinate (handy for calls to SFGetFile, etc.). For alerts, I just call}
  153. {DoAlert, which will center it for me. Note that it adjusts for the menu bar}
  154. {but not for the title bar. This is because my method is used BEFORE calling}
  155. {GetNewDialog, as it tweaks the in-memory copy of the DLOG resource. There's}
  156. {no structure region to peek at at that time. If you're centering the window}
  157. {after its created, you could use the bounding box of the window's structure}
  158. {region, rather than its portRect. That would take care of the title bar as}
  159. {well as anything else.}
  160.  
  161. {Have fun!}
  162. {Michael P. Hecht                 | Internet:  Michael_Hecht@mac.sas.com}
  163. {SAS Institute Inc.; Cary, NC USA | AppleLink: SAS.HECHT}
  164.  
  165. end.